<?php 
//  淘宝登录插件如有BUG请联系本人！！ 
/*===========================================================
*   name : 淘宝
*   author : `Jn.Too'
*   QQ : 274987096
*   E-mail : 274987096@qq.com
*   VERSION : 1.2v
*   DATE : 2011-1-7
*   尊重作者,保留版权信息
*   版权所有 `Jn.Too'
*   使用；不允许对程序代码以任何形式任何目的的再发布。
**/


if (defined('WEBSITE'))
{
	global $_LANG;
	$_LANG['help']['APP_KEY'] = '在open.taobao.com 里申请的 App Key';
	$_LANG['help']['APP_SECRET'] = '请注意填写，最长的就填此处';
	
	$_LANG['APP_KEY'] = 'App Key';
	$_LANG['APP_SECRET'] = 'App Secret';
	
	$i = isset($web) ? count($web) : 0;
	
	// 类名
	$web[$i]['name'] = '淘宝';
	
	
	$web[$i]['className'] = 'taobao';
	
	
	// 文件名，不包含后缀
	$web[$i]['type'] = 'taobao';
	
	// 作者信息
	$web[$i]['author'] = '`Jn.Too\'';
	
	// 作者QQ
	$web[$i]['qq'] = '274987096';
	
	// 作者邮箱
	$web[$i]['email'] = '274987096@qq.com';
	
	// 申请网址
	$web[$i]['website'] = 'http://open.taobao.com';
	
	// 版本号
	$web[$i]['version'] = '1.1v';
	
	// 更新日期
	$web[$i]['date']  = '2012-2-23';
	
	// 配置信息
	$web[$i]['config'] = array(
		array('type'=>'text' , 'name'=>'APP_KEY', 'value'=>''),
		array('type'=>'text' , 'name' => 'APP_SECRET' , 'value' => ''),
	);
}


if (!defined('WEBSITE'))
{
	class website extends cls_http
	{
		var $app_key = '';
		var $app_sess = '';
		
		var $token = '';
		
		
		var $parset = array();  //提交时 附加的参数;
		var $v = '2.0';   //  版本号
		
		
		var $secretKey;
		
		// 构造 函数
		
		function __construct()
		{
			$this->app_key = APP_KEY;
			$this->app_sess = APP_SECRET;
			
			$this->parset = array('client_id' => APP_KEY , 'client_secret' => APP_SECRET);
		}
		
		public function login($retUrl , $state = '1')
		{
			$url = urlencode($retUrl);
			return 'https://oauth.taobao.com/authorize?response_type=code&client_id='.$this->app_key.'&redirect_uri='.$url.'&state='.$url;
		}
		
		function getAccessToken()
		{
			$code = $_REQUEST['code'];
			$par['grant_type'] = 'authorization_code';
			$par['code'] = $code;
			$par['redirect_uri'] = $_REQUEST['state'];
			$token = $this->post('https://oauth.taobao.com/token' , $par);
			
			if(!empty($token['error']))
			{
				$this->error($token['error'] , $token['error_description']);
				return false;
			}
			
			$this->setAccessToken($token);
			return $token;
		}
		
		function setAccessToken($access_token) // 整个数组 放入   getToken 返回的  // 之后就可以 读取API信息了
		{
			$this->parset = array_merge($this->parset , $access_token);
			$this->token = $access_token['access_token'];
			$this->secretKey = $access_token['access_token'];
		}
		
		function get_user_info()  // 不传值 代表 默认为登录用户
		{
			$sys['app_key'] = $this->app_key;
			$sys['v'] = $this->v;
			$sys['format'] = 'json';
			$sys['sign_method'] = 'md5';
			$sys['method'] = 'taobao.user.get';
			
			$sys['timestamp'] = date('Y-m-d H:i:s');
			$sys['partner_id'] = 'top-sdk-php-20111220';
			$sys['session'] = $this->token;
			$msg['fields'] = 'user_id,uid,nick,sex,buyer_credit,seller_credit,location,created,last_visit,birthday,type,status,alipay_no,alipay_account,alipay_account,email,consumer_protection,alipay_bind';
			
			$sys['sign'] = $this->generateSign(array_merge($msg , $sys));
			
			$requestUrl = 'http://gw.api.taobao.com/router/rest?' . http_build_query($sys);
			$ret = $this->http($requestUrl , 'POST' , $msg);
			
			return $this->transferred(json_decode($ret , true));
		}
		
		function transferred($user_info)
		{
			$arr = array();
			if(isset($user_info['error_response']))
			{
				
				$arr = $user_info['error_response'];
				$this->error('用户信息错误' , $arr);
				return false;
			}
			
			
			$user_info = $user_info['user_get_response']['user'];
			
			
			
			$arr['email'] = $user_info['email'];
			$arr['user_id'] = $user_info['user_id'];
			$arr['name']  = $user_info['nick'];
			$arr['location'] = (isset($user_info['location']['state']) ? $user_info['location']['state'] : '' ) .' '. (isset($user_info['location']['city']) ? $user_info['location']['city'] : '');
			$arr['sex'] = (isset($user_info['sex']) ? ($user_info['sex'] == 'm' ? '1' : '0') : 0);
			$arr['rank_id'] = RANK_ID;
			$arr['user_info'] = $user_info;
			return $arr;
		}
		
		function get($url , $parse = array())
		{
			$s = array_merge($this->parset , $parse);
			//$s = $parse;
			$ret = $this->http($url , 'GET' , $s);
			$arr = json_decode($ret , 1);
			return $arr;
		}
		
		function post($url , $parse = array())
		{
			$s = array_merge( $this->parset  ,  $parse);
			$ret = $this->http($url , 'POST' , $s);
			$arr = json_decode($ret , 1);
			return $arr;
		}
		
		function generateSign($params)
		{
			ksort($params);
			$stringToBeSigned = $this->app_sess;
			foreach ($params as $k => $v)
			{
				if("@" != substr($v, 0, 1))
				{
					$stringToBeSigned .= "$k$v";
				}
			}
			$stringToBeSigned .= $this->app_sess;
			return strtoupper(md5($stringToBeSigned));
		}
		
		function error($str , $msg)
		{
			if(is_array($msg))
				$this->add_error($msg['code'], $msg['msg'] , $str);
			else
				$this->add_error($str , $msg);
		}
	}
}
?>